package com.itheima.reggie.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/** @Author dml
 * Date on 2022/6/6 0006
 */


@WebFilter(filterName = "LoginCheckFilter", urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        // 获取本次请求URI
        String requestURI = request.getRequestURI();
        log.info("拦截到请求:{}", requestURI);
        String[] urls = new String[]{
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/common/**",
                "/user/sendMsg",
                "/user/login"
        };
        boolean check = check(urls, requestURI);
        if (check) {
            log.info("本次请求{}不需要处理:", requestURI);
            filterChain.doFilter(request, response);
            return;
        }
        if (request.getSession().getAttribute("employee") != null) {
            log.info("用户已登录,用户id为:{}", request.getSession().getAttribute("employee"));
            // 获取用户Id
            Long empId = (Long)request.getSession().getAttribute("employee");
            // 通过session 根据用户抽取出id 在同一线程之内 推到BaseContext内 实现 同一线程内 内容共享
            BaseContext.setCurrentId(empId);
            long id = Thread.currentThread().getId();
            log.info("线程Id为:",id);

            filterChain.doFilter(request, response);
            return;
        }
        // 用户id
        if (request.getSession().getAttribute("user") != null) {
            log.info("用户已登录,用户id为:{}", request.getSession().getAttribute("user"));
            // 获取用户Id
            Long UserId = (Long)request.getSession().getAttribute("user");
            // 通过session 根据用户抽取出id 在同一线程之内 推到BaseContext内 实现 同一线程内 内容共享
            BaseContext.setCurrentId(UserId);
            long id = Thread.currentThread().getId();
            System.out.println("线程id为:"+id);
            log.info("线程Id为:",id);
            // 放行
            filterChain.doFilter(request, response);
            return;
        }
        log.info("用户未登录");
        // 如果未登录则返回未登录结果，通过输出流方式向客户端页面响应数据
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;
    }

    public boolean check(String[] urls, String requestURI) {
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requestURI);
            if (match) {
                return true;
            }
        }
        return false;
    }
}


